package com.scalar.forecast.forecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastEntry.forms.ForecastEntrySearchForm;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.forecastEntry.SearchFieldListComparator;
import com.scalar.forecast.forecastEntry.SearchField;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Calendar;

/**
 * User: Sujan Kumar
 * Date: Feb 21, 2005
 * Time: 9:11:10 PM
 */
public class ForecastEntrySearchBuildLoadValuesCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ForecastEntrySearchForm forecastEntrySearchForm = ( ForecastEntrySearchForm )form;
        HashMap errors = new HashMap();
        ArrayList searchFieldList = new ArrayList();
        try
        {
            ArrayList forecastSetupFields = ForecastSetupDAO.getForecastSetupFields(null);
            request.setAttribute ( "forecastSetupFields", forecastSetupFields );
			String orderBy = forecastEntrySearchForm.getOrderBy();
			if ( orderBy == null || orderBy.trim().length() == 0 )
			{
				forecastEntrySearchForm.setOrderBy( ForecastEntrySearchNewCommand.getDefaultOrderBy ( forecastSetupFields ) );
				ForecastTimeFrame timeframe = ForecastSetupDAO.getForecastTimeFrame(null);
				Calendar  now = Calendar.getInstance();
				if ( Constants.FORECAST_TIMEFRAME_MONTH.equalsIgnoreCase( timeframe.getTfName() ) )
				{
					forecastEntrySearchForm.setStartMonth_Quarter_Year( now.get( Calendar.MONTH ) );
				}
				else if ( Constants.FORECAST_TIMEFRAME_QUARTER.equalsIgnoreCase( timeframe.getTfName() ) )
				{
					int currentQuarter = -1;
					try
					{
						currentQuarter = ForecastEntrySearchDAO.getCurrentQuarter(null);
					}
					catch ( Exception e)
					{
						System.out.println ( "--Exception in getting the current Quarter---" );
						e.printStackTrace();
					}
					forecastEntrySearchForm.setStartMonth_Quarter_Year( currentQuarter );
				}
				else if ( Constants.FORECAST_TIMEFRAME_YEAR.equalsIgnoreCase( timeframe.getTfName() ) )
				{
					forecastEntrySearchForm.setStartMonth_Quarter_Year( now.get( Calendar.YEAR ) );
				}
			}
            searchFieldList = ForecastEntrySearchDAO.getForecastSetupSearchFields(null);
            int searchFieldListSize = searchFieldList.size();
            SearchField searchField = null;
            //for not to display showNext in the searchfield
            for ( int i=0; i < searchFieldListSize; i++ )
            {
                searchField = ( SearchField )searchFieldList.get( i );
                if ( "showNext".equalsIgnoreCase( searchField.getName() ) )
                {
                    searchField.setDisplay( false );
                    break;
                }
            }
            ForecastEntrySearchNewCommand.loadSearchFieldListPropertyValues(forecastEntrySearchForm,searchFieldList);
            //adding new Fields to the search screen
            searchField = new SearchField();
            searchField.setName( "salesPersonId" );
            searchField.setSortOrder( 8 );
            searchField.setDisplayName ( "Sales Person" );
            searchField.setType ( "select" );
            searchField.setDisplay( true );
            searchField.setPropertyName( "salesPersonId" );
            LabelValueBean labelValueBean = new LabelValueBean( "---Select SalesPerson---","-1");
            ArrayList propValues = new ArrayList();
            propValues = ForecastEntrySearchDAO.getAllForecastSalesPersons( null );
            propValues.add (0, labelValueBean );
            searchField.setPropertyValues( propValues );

            searchFieldList.add ( searchField );

            searchField = new SearchField();
            searchField.setName( "buildNo" );
            searchField.setSortOrder( 9 );
            searchField.setDisplayName ( "Build Number" );
            searchField.setType ( "text" );
            searchField.setDisplay( true );
            searchField.setPropertyName( "buildNo" );
            searchFieldList.add ( searchField );

        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_SEARCH_FIELDS );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception in search new Command:" );
            e.printStackTrace();
            return mapping.findForward ( "errorPage" );
        }
        ForecastEntrySearchNewCommand.setSortOrder ( forecastEntrySearchForm.getOrderBy(), searchFieldList );
        Collections.sort( searchFieldList, new SearchFieldListComparator() );
        forecastEntrySearchForm.setSearchFieldList( searchFieldList );
        ActionForward actionForward = mapping.findForward ( "searchBuildPage" );
        return actionForward;
    }
}
